<?php


namespace app\manage\controller;

use app\common\model\Verification as VerificationModel;
use app\common\model\Members as MembersModel;
use think\Db;

class Verification extends Base
{
    public function cancelBinding(){
        Db::startTrans();
        try {
            $id = input('id');
            $data = VerificationModel::get($id);
            $member = MembersModel::get($data['member_id']);
            $member->is_binding_verification = 0;
            if ($member->save()){
                $data->member_id = 0;
                $data->is_binding = 0;
                if ($data->save()){
                    Db::commit();
                    return j(0,"操作成功");
                }
            }
            Db::rollback();
            return j(1,"操作失败");
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return j(1,"操作失败");
        }
    }
    public function deleteVerification(){
        Db::startTrans();
        try {
            $id = input('id');
            $data = VerificationModel::get($id);
            if (!$data['is_binding']){
                $data->delete();
                Db::commit();
                return j(0,"操作成功");
            }
            $member = MembersModel::get($data['member_id']);
            $member->is_binding_verification = 0;
            if ($member->is_binding_verification == 0){
                if ($data->delete()){
                    Db::commit();
                    return j(0,"操作成功");
                }
            }
            if ($member->save()){
                if ($data->delete()){
                    Db::commit();
                    return j(0,"操作成功");
                }
            }
            Db::rollback();
            return j(1,"操作失败");
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return j(1,"操作失败");
        }
    }
}